﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.Text;
using ReflectionStudio.Core.Events;
namespace WPFExcelReport
{

    public class VK02AV95Data : BaseFunction
    { 
        public VK02AV95Data()
            : base()
        {

        }
        public DataSet LoadTableStructure(DataSet dsTarget)
        {
            try
            {
                TableQueue.Enqueue("WAFARBEN");
                TableQueue.Enqueue("SELECT WAFART as ArtNum,	WAFVAR as ArtVar,	 WAFZUSATZ as ArtColor,WAFBEZ as ColorDescription"
                    + "  FROM INTEX.WAFARBEN WHERE RowNum<1");
                TableQueue.Enqueue("BSSATZ");
                TableQueue.Enqueue("SELECT  "
                    + "BSKART"
                    + ",BSFDZ"
                    + ",BSSRFNR"
                    + ",BSFAKTOR"
                   + " FROM INTEX.BSSATZ WHERE RowNum<1");
                TableQueue.Enqueue("BASTKE");
                TableQueue.Enqueue("SELECT "
                              + "BAKART"
               + ",BAKVAR"
               + ",BAKFARBE"
               + ",BAKKART"
               + ",BAKKVAR"
               + ",BAKKFARBE"
               + ",BAKPATT"
               + ",BAKPOS"
               + ",BAKANZ"
               + ",BAKABF"
               + " FROM INTEX.BASTKE WHERE RowNum<1");
                TableQueue.Enqueue("BSFARB");
                TableQueue.Enqueue("SELECT  "
                    + "BSFKART"
                    + ",BSFKVAR"
                    + ",BSFKFARBE "
                    + ",BSFBEZ"
                + " FROM INTEX.BSFARB WHERE RowNum<1");
                TableQueue.Enqueue("BASTGA");
                TableQueue.Enqueue("SELECT "
                + "BAGFDZ"
                + ",BAGART"
                + ",BAGVAR"
                + ",BAGFARBE"
                + ",BAGPOS"
                + ",BAGGART"
                + ",BAGGVAR"
                + ",BAGGFARBE"
                + ",BAGFDZ"
                + ",BAGKGNET"
                + ",BAGKGBRUT"
                + " FROM INTEX.BASTGA WHERE RowNum<1");
                TableQueue.Enqueue("WASTGA");
                TableQueue.Enqueue("SELECT  "
                       + "WAGART"
                + ",WAGVAR"
                + ",WAGWCOL"
                + ",WAGPSA"
                + ",WAGPOS"
                + ",WAGGART"
                + ",WAGGVAR"
                + ",WAGGFARBE"
                + ",WAGFDZ"
                + ",WAGKGNET"
                + ",WAGKGBRUT"
                + ",WAGEAFAKTOR"
                + ",WAGBEM"
                + " FROM INTEX.WASTGA WHERE RowNum<1");
                TableQueue.Enqueue("GSDAT");
                TableQueue.Enqueue("SELECT "
                + " GSART"
                + " ,GSVAR"
                + " ,GSMAT"
                + " ,GSNUMART"
                + " ,GSGARNNR"
                + " ,GSFACH"
                + " ,GSRECHNUM"
                + " ,GSRECHNR"
                 + " FROM INTEX.GSDAT WHERE RowNum<1");
                TableQueue.Enqueue("GASTGA");
                TableQueue.Enqueue("SELECT "
                    + "GAGART"
                    + ",GAGVAR"
                    + ",GAGFARBE"
                    + ",GAGPOS"
                    + ",GAGGART"
                    + ",GAGGVAR"
                    + ",GAGGFARBE"
                    + ",GAGKGNET"
                    + ",GAGKGBRUT"
                   + " FROM INTEX.GASTGA Where RowNum<1");
                TableQueue.Enqueue("GSCOLOR");
                TableQueue.Enqueue("SELECT GSCART,GSCVAR,GSCFARBE,GSCBEZ FROM INTEX.GSCOLOR WHERE RowNum<1");
                while (TableQueue.Count > 0)
                {
                    string TableName = TableQueue.Dequeue() as string;
                    string QueryStr = TableQueue.Dequeue() as string;
                    if (!dsTarget.Tables.Contains(TableName))
                    {
                        orlReadCommand = OracleDB.CreateCommand();
                        orlReadCommand.CommandText = QueryStr;
                        dsResult = Connect2Proxy(orlReadCommand);
                        if (dsResult.Tables.Count > 0)
                        {
                            dsResult.Tables[0].TableName = TableName;
                        }
                        foreach (DataTable dtTemp in dsResult.Tables)
                        {
                            dsTarget = ImportDataTable(dsTarget, dtTemp);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return dsTarget;
        }
        public VK02AV95Data(Dictionary<string, object> DicGlobalPara)
            : base(DicGlobalPara)
        {
        }
        public DataSet GetVKPOS(DataRow dwVKPOS)
        {
            try
            {
                setReturn = new DataSet();
                orlReadCommand = OracleDB.CreateCommand();
                orlReadCommand.CommandText = "SELECT DISTINCT VKPAUFKRS,VKPAUFLFD,VKPAUFPOS,VKPKUNDNR,VKPPOSTYP,VKPART,VKPFARBE,VKPWUTERM,VKPMENGE1,VKPRESERV,VKPZUGET,VKPMEEH,VKPNEUBEN,VKPNEUDAT "
                         + " FROM INTEX.VKPOS WHERE ROWNUM<1000 AND VKPSA<>'AJ' AND VKPERL=' '  and VKPRESERV>:VKPRESERV "
                         + " ORDER BY VKPWUTERM";
                orlReadCommand.Parameters.Add("VKPRESERV", OracleType.NVarChar).Value = dwVKPOS["VKPRESERV"].ToString();
                setResult = Connect2Proxy(orlReadCommand);

                if (setResult.Tables.Count > 0)
                {
                    tableResult = setResult.Tables[0];
                    tableResult.TableName = "VKPOS";
                    Queue Q100 = new Queue();
                    Q100.Enqueue("VKPMENGE1");
                    Q100.Enqueue("VKPRESERV");
                    Q100.Enqueue("VKPZUGET");
                    DivideBy100(tableResult, Q100);
                }
                byte[] b;
                string value;
                foreach (DataRow dwTemp in tableResult.Rows)
                {
                    b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dwTemp["VKPFARBE"].ToString().Trim());
                    value = Encoding.GetEncoding("gb2312").GetString(b);
                    dwTemp["VKPFARBE"] = value;
                }
                setReturn.Merge(setResult);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex); ;
            }
            return setReturn;
        } 
        public DataSet BuildVKPOS(DataRow dwVKPOS)
        {
            try
            { 
                setReturn = new DataSet();
                setResult = GetVKPOS(dwVKPOS);
                setReturn.Merge(setResult);  
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return setReturn;
        }
      

    }
}